"use strict";
const Generator = require("yeoman-generator");
const chalk = require("chalk");
const yosay = require("yosay");

module.exports = class extends Generator {
  constructor() {
    super(...arguments);

    this.answers = {};
  }

  /**
   * Print welcome message
   */
  initializing() {
    this.log(yosay("React自定义组件CLI"));
    this.log(
      chalk.magenta(
        `欢迎您` +
          "\n" +
          "这是generator生成lerna react自定义组件CLI, Powered by https://docs.zkkysqs.top." +
          "\n"
      )
    );
  }

  /**
   * Interact with developer.
   */
  prompting() {
    const prompts = [
      {
        type: "confirm",
        name: "someAnswer",
        message: "您想继续吗？",
        default: true
      },
      {
        type: "input",
        name: "name",
        message: "项目名称",
        default: this.appname
      },
      {
        type: "input",
        name: "author",
        message: "项目开发者",
        store: true,
        default: ""
      },
      {
        type: "input",
        name: "version",
        message: "项目版本号",
        default: "0.0.1"
      },
      {
        type: "input",
        name: "description",
        message: "项目描述",
        default: ""
      }
    ];

    return this.prompt(prompts).then(answers => {
      // To access props later use this.props.someAnswer;
      this.answers = answers;
    });
  }

  /**
   * Copy templates
   */
  writing() {
    const { name } = this.answers;
    this.destinationRoot(this.destinationPath(name));

    this.fs.copyTpl(
      `${this.templatePath()}/**/!(_)*`,
      this.destinationPath(),
      this.answers,
      {},
      { globOptions: { dot: true } } // Copy all dots files.
    );
  }

  /**
   * Install dependencies
   */
  install() {
    console.log("安装依赖中。。。");
    // 使用npm安装依赖
    // this.npmInstall();
    // 使用yarn安装依赖
    this.yarnInstall();
  }

  /**
   * Prompt user to start project
   */
  end() {
    const { name } = this.answers;

    console.log("本次初始化过程结束, 请通过以下命令运行项目: ");
    console.log();
    console.log(chalk.cyan("  cd"), name);
    console.log(`  ${chalk.cyan("yarn start")}`);
    console.log();
    console.log("编码愉快!");
  }
};
